package com.hdax.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdax.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121624294";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMkO1WY9eH+29y6O892fh5c77Bg9NHIhZGlUc7lnNwpHK8kNLaTyMmdjoFqLHtBnZBnNFUwPJqk/h4UegIaTklfIYDPRR7o82AM31NJPej+fWkwSVJDsR4Vhjt2FJ75Q25KruvL76Ayy/cYsJE18ozQ3GFe+DMjo1zMdybd9p5L9t+m1XoVc6XWFMomjpWrhxCkW+AGAkC+eJiK8A8lFZw9OplpslOtD+Mc4FsASWHq5m+gGMmYiTznidfJcJ4y9bYf+/3Uc7/cQG03G02n+kcMhuyNoChPhBEsKFZmjbjueTzNj3mnhCINo7JHxq2Eizu0VO2iQmb0ka0TtuxB5eZAgMBAAECggEAFLtzclf4Fg4Dg4/b/1bB+f0jDhXAKetoeorSh1V+KaJhBn9e/0UZ+OHui3hHYBBRl+C+zmonp2mLuxYCACg55HS0WEuuccFuKFoPjuv6NSI2JRZ2IuteOhvvtts/sNLMC0Ird1vNYNw2440Wx0dfdMaXEMI3TEP+Tfh1tUP2XmWu4kXYd4rjcE5y/VSdUX4AwXpM2Y7hmVEYf5tgzL24YssfWxEjUfPE13zDvpijGLozsFbZTzTVpmHEcvzt34xJ77ePQ3rq9PqvtnJd19V9a8yCM2rf47sffiz/9hYlD616x2Ag0JjgMP9sMq/a1jeKYqpWe4xu7d7AcFHIBq2SCQKBgQDpM7MvyTw+OEX6TLXyaJrWeDAxGoJDXFf3Jl6DS1DyYXcvLeRs7CAIpssxB81c5becLr/BPImajm29djjHVxnRRTGUtEmpSLp/TIhxW+PWlHIeLUUVYwpGHuyFYsLEln70VEoFkw43AplYf/vNAjR3crQOX6Gye/NxlpNBqUXGqwKBgQDgkJBSAa2bWTeu+LyiWMmR0QuqoUJl+wOXzOF+7DuP0zq9D4BwJ2mh/0PTE1M/D10yHN/xTyFaZsDW4RZD9XE2hjiUpnb6oIawnW/M5tzcwJCBffbDtqXAYn1+b/A5Q/WLruvng13eOqAEo3rQSD2UaEaLVlQqjrs1C8NzeFEqywKBgGNor4zCStT3JhVx1+nS35ZnO5vjAf53AeSQTZ/IXmREWgsZlBM+gNVPTLTE760XG7MPVpyoErx41Jz8JW4MWiwUQ/Nw+ymXoDCdzlgMZM5XD2J/x8A7LOkBS6/8jqxAvzOmLru7mUan6/iG7i7RLs5wbgLmj0ZHglo8Zt+Jqf3rAoGAROMT4pQxNV6A/mO/Oe4Sp3CBK66cN7xwtbhb1BuTieM6OZTvyXfNp6wumilreAS2ihDhgZzMlna+4SvbMK2yNQ+ne7I0/V0Q9EzXZvvK9Mz9hzuk3xTAc+vCo26jOm3mfRsDstlRikJhpAqiOhJvEX+LZGLvHr76HqOlKV7XR1ECgYEA238BlHhirMTw0ga6o0Zic7Yotmh8iCAgcFhp+WjP8y1Wsv6CGACwwbcTwRHzhSAXlF5J36uA/HzRzWXnzbsEdpN+VAyuwsfmUo4ZVLDWQBHQtxcoyIqAV0NRLW3+6kmrc4j9f3M23OkbPyz0myR/lc4Ll0B5z50owByO+yf0KJI=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkLQjJWLLnjw4fF3mH8wDkM0zg6JvVNYa+qTuF664VRqUNOwOyNaJ9lLl4tNFugnteQvEWyM5KfiFMW63Jqn1mA+kMwTM/ssDjF3OIVxi223lZmM3Kvy3iP9xzRpDCVjN0u4LQyUpVRKdZkLE/ddaQqavRVwaLUv0OcxuC4KCzk6zprJcGxH+8ae1WrhH84Uh03foDWebk4YNWSt4S6EkFdUn2DEdC3Ff/0Xlwp+MPA/mOhdG8rWSW8wwexwhCxRu7n5dVhGHXk1yoijHyg+NC0cEec7eZL1EfjoGbceUxffwNrC2jN856qHP5DOyLnG5s7XCRvnRAnnZTU2qTz5jtQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://74e58646.r12.cpolar.top/pay/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    private String time_out= "1m";
    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"timeout_express\":\""+ time_out +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
